package com.ddwl.order.service;

import com.ddwl.common.constant.OrderEnum;
import com.ddwl.common.constant.StatisticsEnum;
import com.ddwl.common.feign.api.GateApi;
import com.ddwl.common.feign.api.GoodsApi;
import com.ddwl.common.feign.api.UserApi;
import com.ddwl.common.feign.api.WalletApi;
import com.ddwl.order.dao.AfterSaleOrderDao;
import com.ddwl.order.dao.OrderInfoDao;
import com.ddwl.order.dao.ThirdPartRecordDao;
import com.ddwl.schema.bo.goods.ItemListBo;
import com.ddwl.schema.bo.goods.QueryAcademyBo;
import com.ddwl.schema.bo.order.QueryAfterSaleOrderListBo;
import com.ddwl.schema.bo.order.QueryOrderListBo;
import com.ddwl.schema.bo.user.ProfitApplyQueryBo;
import com.ddwl.schema.bo.wallet.WithdrawListQueryBo;
import com.ddwl.schema.vo.order.BackLogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 待办事项
 * @author wenbin
 */
@Service
@RefreshScope
@Slf4j
public class BacklogServer{

    @Resource
    ThirdPartRecordDao thirdPartRecordDao;
    @Resource
    private OrderInfoDao orderInfoDao;
    @Resource
    private AfterSaleOrderDao afterSaleOrderDao;
    @Resource
    private GoodsApi goodsApi;
    @Resource
    private GateApi gateApi;
    @Resource
    private WalletApi walletApi;
    @Resource
    private UserApi userApi;

    public BackLogVo getBacklog() {

        BackLogVo backLogVo=new BackLogVo();
        /**
         * 1，未发货订单
         */
        Integer unSendCount = queryUnSendCount();
        backLogVo.setUnSendCount(unSendCount==null?0:unSendCount);

        /**
         * 2，未处理的提现申请
         */
        Integer unApproveWithdrawApply = queryUnApproveWithdrawApply();
        backLogVo.setUnApproveWithdrawApply(unApproveWithdrawApply==null?0:unApproveWithdrawApply);

        /**
         * 3，未审核的文章
         */
        Integer unApproveArticles = queryUnApproveArticles();
        backLogVo.setUnApproveArticles(unApproveArticles==null?0:unApproveArticles);

        /**
         * 4，未审批的身份证
         */
        Integer unApproveIdCard = queryUnApproveIdCard();
        backLogVo.setUnApproveIdCard(unApproveIdCard==null?0:unApproveIdCard);

        /**
         * 5，上架状态商品0库存
         */
        Integer zeroCountItem = queryZeroCountItem();
        backLogVo.setZeroCountItem(zeroCountItem==null?0:zeroCountItem);

        /**
         * 6，未处理售后订单
         */
        Integer pendingAfterSaleCount = queryPendingAfterSaleCount();
        backLogVo.setPendingAfterSaleCount(pendingAfterSaleCount==null?0:pendingAfterSaleCount);

        return  backLogVo;
    }


    /**
     * 未发货订单
     */
    public Integer queryUnSendCount(){
        QueryOrderListBo bo=new QueryOrderListBo();
        bo.setStatus("1");
        Integer unSendCount = orderInfoDao.queryOrderCount(bo);
        return unSendCount;
    }

    /**
     * 需要客服处理的售后订单
     */
    public Integer queryPendingAfterSaleCount(){
        QueryAfterSaleOrderListBo bo=new QueryAfterSaleOrderListBo();
        List<Integer> status =new ArrayList<>();
        status.add(OrderEnum.AfterStatus.APPLY.getCode());
        status.add(OrderEnum.AfterStatus.SEND.getCode());
        bo.setAfterSaleBatch(status);
        Integer afterSaleOrderCount = afterSaleOrderDao.queryAfterSaleOrderCount(bo);
        return afterSaleOrderCount;
    }



    /**
     * 未处理的体现申请
     */
    public Integer queryUnApproveWithdrawApply(){
        WithdrawListQueryBo bo=new WithdrawListQueryBo();
        bo.setStatus(0);
        Integer UnApproveWithdrawApplyCount = walletApi.queryWithdrawRecordCount(bo).getResult();
        return UnApproveWithdrawApplyCount;
    }

    /**
     * 未审批的身份证
     */
    public Integer queryUnApproveIdCard(){
        ProfitApplyQueryBo pageBo=new ProfitApplyQueryBo();
        pageBo.setStatus(10);
        Integer unApproveIdCardCount = userApi.queryProfitApplyCount(pageBo).getResult();
        return unApproveIdCardCount;
    }

    /**
     * 未审核的文章
     */
    public Integer queryUnApproveArticles(){
        QueryAcademyBo pageBo=new QueryAcademyBo();
        pageBo.setStatus(3);
        Integer unApproveArticles = goodsApi.academyUnApproveList(pageBo).getResult();
        return unApproveArticles;
    }

    /**
     * 上架状态商品0库存
     */
    public Integer queryZeroCountItem(){
        ItemListBo pageBo=new ItemListBo();
        pageBo.setItemStatus(1);
        pageBo.setMaxStock(0);
        Integer zeroCountItem = goodsApi.querySkuWithItemCount(pageBo).getResult();
        return zeroCountItem;
    }



    public void sendBackLogMsg() throws Exception {
        BackLogVo backLogVo = getBacklog();
        StringBuffer message=new StringBuffer();
        message
                .append("每日待处理数据统计:,")
                //未发货订单
                .append(StatisticsEnum.UN_SEND_COUNT).append(":")
                .append(backLogVo.getUnSendCount()).append(",")
                //未处理的提现申请
                .append(StatisticsEnum.UN_APPROVE_WITHDRAW_APPLY).append(":")
                .append(backLogVo.getUnApproveWithdrawApply()).append(",")
                //未审核的文章
                .append(StatisticsEnum.UN_APPROVE_ARTICLES).append(":")
                .append(backLogVo.getUnApproveArticles()).append(",")
                //未审批的身份证
                .append(StatisticsEnum.UN_APPROVE_IDCARD).append(":")
                .append(backLogVo.getUnApproveIdCard()).append(",")
                //上架状态商品0库存
                .append(StatisticsEnum.ZERO_COUNT_ITEM).append(":")
                .append(backLogVo.getZeroCountItem()).append(",")
                //需要客服处理的售后订单
                .append(StatisticsEnum.PENDING_AFTERSALE_COUNT).append(":")
                .append(backLogVo.getPendingAfterSaleCount()).append(",");


        //具体需要提醒的人的电话,","分隔
        String mobiles="";
        gateApi.sendDingTalkMessageMobiles(message.toString(),mobiles);
    }


    public void deleteKuiamaiLog() {
        thirdPartRecordDao.deleteLog();
    }
}
